﻿/*
 * 作者：黄港
 * 功能描述：实现用户操作
 * 时间：2018-1-17
 * 修改人：
 * 修改时间：
 */
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.OleDb;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Security.Cryptography;
using System.Threading.Tasks;
using System.Windows.Forms;

using DataBusiness;
using DevExpress.XtraEditors.Repository;
using Entity;
using DevExpress.XtraEditors.Controls;
using DevExpress.XtraEditors;
using DevExpress.XtraGrid.Views.Grid;

namespace UI
{

    public partial class frmUserInfo : Form
    {
        public frmUserInfo()
        {
            InitializeComponent();
        }

        public DataTable gridview_Source()
        {

            var t = grdUser.DataSource;
            DataTable dt = this.bindSource.DataSource as DataTable;
            //string str= grdUser.GetRowCellDisplayText(1, grdUser.Columns[1]).ToString();
            //for (int i=0;i<grdUser.RowCount;i++)
            //{
            //    for(int j=0;j<grdUser.Columns.Count;j++)
            //    {
            //        dt.Rows[i][j] = grdUser.GetRowCellValue(i, grdUser.Columns[j]);
            //    }
            //}
            return dt;
        }
        /// <summary>
        /// 取消单元格编辑右键弹出的固有菜单
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void grdview_ShownEditor(object sender,EventArgs e)
        {
           ContextMenu emptyMenu =new ContextMenu();
           GridView view = sender as GridView;
           view.ActiveEditor.ContextMenu = emptyMenu;
        }

        /// <summary>
        /// 绑定repositoryItemComboBox1的值
        /// </summary>
        private void RicomboxBindData()
        {
            repositoryItemComboBox1.Items.Add("管理员");
            repositoryItemComboBox1.Items.Add("设计人员");
            repositoryItemComboBox1.Items.Add("普通人员");
            //this.repositoryItemComboBox1.ParseEditValue += new ConvertEditValueEventHandler(RiComboBox_ParseEditValue);
            //for (int i=0;i<3;i++)
            //{

            //}            
        }


        ///<summary>
        /// 用户类型下拉框选中改变事件
        /// </summary>
        ///<param name="e"></param>
        ///<param name="sender"></param>
        private void ComboBoxEdit_SelectedIndexChanged(object sender, EventArgs e)
        {
            RiCombox Rcb = new RiCombox();
            try
            {
                //获取下拉框的值
                Rcb = (RiCombox)(sender as ComboBoxEdit).SelectedItem;
                string Text = Rcb.Text.ToString();
                string Value = Rcb.Value.ToString();
                //获取gridview选中的行
                GridView grdview = (grdUser as GridView);
                int index = grdview.GetDataSourceRowIndex(grdview.FocusedRowHandle);
                //保存选中值
                DataTable dt = (DataTable)this.grdUser.DataSource;
                dt.Rows[index]["用户类型"] = Text;
            }
            catch (Exception ex)
            {
                XtraMessageBox.Show(ex.Message, "提示");
            }
        }


        /// <summary>
        /// 解决IConvertible问题  
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void RiComboBox_ParseEditValue(object sender, ConvertEditValueEventArgs e)
        {
            try
            {
                e.Value = e.Value.ToString();
            }
            catch(Exception Ex)
            {
                e = new ConvertEditValueEventArgs();
                e.Value = "";
            }
            e.Handled = true;
        }


        private void BindData()
        {
            bindSource.DataSource = UserBLL.GetUsers(txtName.Text, cmbType.Text).Tables[0];
            this.gclUser.DataSource = bindSource.DataSource;
            grdUser.AddNewRow();
            //grdUser.Columns[0].Visible = false;
            //grdUser.Columns[3].Visible = false;
            //grdUser.Columns[8].Visible = false;
            //grdUser.Columns[9].Visible = false;
            //grdUser.OptionsView.ColumnAutoWidth = true;
            //grdUser.BestFitColumns();//数据自动适应行宽
            //gridView1.PopulateColumns();

        }
        private void UserManagement_Load(object sender, EventArgs e)
        {
            BindData();//为grdview绑定数据源
            RicomboxBindData();//为表格下拉框绑定数据
        }


        /// <summary>
        /// 导出数据成.xls文件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnExport_Click(object sender, EventArgs e)
        {
            try
            {
                if (grdUser.RowCount == 0)
                {
                    MessageBox.Show("该表中没有数据，不能导出！", "提示");
                }
                using (SaveFileDialog saveFD = new SaveFileDialog())
                {
                    saveFD.Filter = "导出Excel(*.xls)|*.xls";
                    saveFD.FilterIndex = 0;
                    saveFD.RestoreDirectory = true;
                    saveFD.CreatePrompt = true;
                    saveFD.Title = "导出文件保存路径";
                    //默认文件名
                    saveFD.FileName = "";
                    if (saveFD.ShowDialog() == DialogResult.OK)
                    {
                        DevExpress.XtraPrinting.XlsExportOptions options = new DevExpress.XtraPrinting.XlsExportOptions();
                        grdUser.ExportToXls(saveFD.FileName);
                        DevExpress.XtraEditors.XtraMessageBox.Show("导出成功！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "提示");
            }
        }


        /// <summary>
        /// 按条件查询数据
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnInquiry_Click(object sender, EventArgs e)
        {
            BindData();
        }

        private void cmsAdd_Click(object sender, EventArgs e)
        {
            grdUser.AddNewRow();//新建增加一行
        }

        /// <summary>
        /// 删除选中行
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void cmsDelete_Click(object sender, EventArgs e)
        {
            try
            {
                int t = grdUser.GetSelectedRows()[0];//获取选中行的行号
                string uAccount = grdUser.GetRowCellValue(t, "UserAccount").ToString();//获取改行中UserName列中的数据
                string uId = grdUser.GetRowCellValue(t, "UserID").ToString();
                if (MessageBox.Show("确定要删除" + uAccount + "的记录吗？", "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Warning,
                    MessageBoxDefaultButton.Button2, 0, false) == DialogResult.Yes)
                {
                    if (UserBLL.DeleteUser(uId) > 0)
                    {
                        MessageBox.Show(uAccount + "的记录已经被删除！", "提示");
                    }
                }
            }
            catch (Exception ex)
            {
                XtraMessageBox.Show("删除失败！没有可以删除的记录！", "提示");
            }
        }

        private void cmsRefresh_Click(object sender, EventArgs e)
        {
            BindData();
        }

        /// <summary>
        /// 重置密码
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnResetPwd_Click(object sender, EventArgs e)
        {
            try
            {
                int t = grdUser.GetSelectedRows()[0];//获取选中行的行号
                //MessageBox.Show(grdUser.GetRowCellValue(t,"UserID").ToString());
                string uAccount = grdUser.GetRowCellValue(t, "UserAccount").ToString();
                string uId = grdUser.GetRowCellValue(t, "UserID").ToString();
                if (MessageBox.Show("确定要重置" + uAccount + "的密码吗？", "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Warning, MessageBoxDefaultButton.Button2, 0, false) == DialogResult.Yes)
                {
                    if (UserBLL.ResetPwd(uId, uAccount) > 0)
                    {
                        MessageBox.Show(uAccount + "的密码已经被重置!", "提示");
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("重置失败！没有记录可以重置密码！", "错误提示");
            }
        }


        /// <summary>
        /// 自动添加新行
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void grdUser_MouseUp(object sender, MouseEventArgs e)
        {
            int count = grdUser.RowCount;
            string uAccount = grdUser.GetRowCellValue(count-1, "UserAccount").ToString();
            string uName = grdUser.GetRowCellValue(count-1, "UserName").ToString();
            string uType = grdUser.GetRowCellValue(count-1, "UserType").ToString();
            if(uAccount!= "" && uName != "" && uType != "")
            {
                grdUser.AddNewRow();
            }
        }
    }

}